﻿using System;
using System.Xml.Linq;

namespace CodeCounsel.SharePoint.RibbonDesigner.Model
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class XNameAttribute
        : Attribute
    {
        public string Name { get; set; }
        public string Namespace { get; set; }

        public XNameAttribute()
        {
        }

        public XNameAttribute(string name)
        {
            Name = name;
        }

        public XNameAttribute(string name, string namespaceName)
        {
            Name = name;
            Namespace = namespaceName;
        }

        public XName ToName()
        {
            return String.IsNullOrEmpty(Namespace) ?
                (XName)Name :
                XName.Get(Name, Namespace);
        }

        public static XName GetName(Type type)
        {
            XNameAttribute attribute = (XNameAttribute)Attribute.GetCustomAttribute(
                type, typeof(XNameAttribute));
            return attribute.ToName();
        }
    }
}
